#!/usr/bin/python
# -*- coding: utf-8 -*-

#-----------------------------------------------------------------------------
# Name:        
# Purpose:     
#
# Author:      Administrator
#
# Created:     2006-11-29 下午05:16:49
# RCS-ID:      Id:  
# Copyright:   (c) 2006
# Licence:     Free To Use
# 
#-----------------------------------------------------------------------------

import os
import os.path
import sys
import tempfile
import win32api
import win32con
import wx

USE_PROXY = True
PROXY_CAP = 'freecapConW.exe'

class GameRunnerAcctError(Exception):    
    '''raise a game runner Account Error
    '''
    
    pass

class GameRunnerProxyError(Exception):
    '''raise a game runner proxy error
    '''
    
    pass

class GameRunnerError(Exception):
    '''raise a game runner error
    '''
    
    pass

class GameRunner:
    '''Game runner, use autoitX dll file
    '''
    
    def __init__(self, g='USWOW'):
        global GR
        self.game_name = g
        if self.game_name == 'USWOW':
            import GameRunnerWOW as GR
        elif self.game_name == 'CNWOW':
            import GameRunnerWOW as GR
        else:
            pass
        #print dir(GR)
        self.game_acct_name = ''
        self.game_acct_pass = ''
        self.game_proxy_host = ''
        self.game_proxy_port = ''
        self.game_proxy_user = ''
        self.game_proxy_pass = ''

    def build_conf(self):
        '''set freecap reg infomation
        '''
        
        win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, '''SoftWare\\Bert's Software''')
        win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, '''SoftWare\\Bert's Software\\FreeCap''')
        win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, '''SoftWare\\Bert's Software\\FreeCap\\Programs''')
        win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, '''SoftWare\\Bert's Software\\FreeCap\\SocksChain''')
        
        freecap_main_keys = win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, '''SoftWare\\Bert's Software\\FreeCap\\Main''')
        win32api.RegSetValueEx(freecap_main_keys, "ViewStyle", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "Width", 0, win32con.REG_SZ, "589")
        win32api.RegSetValueEx(freecap_main_keys, "Height", 0, win32con.REG_SZ, "449")
        win32api.RegSetValueEx(freecap_main_keys, "Left", 0, win32con.REG_SZ, "217")
        win32api.RegSetValueEx(freecap_main_keys, "Top", 0, win32con.REG_SZ, "159")
        win32api.RegSetValueEx(freecap_main_keys, "LogHeight", 0, win32con.REG_SZ, "88")
        win32api.RegSetValueEx(freecap_main_keys, "Log", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "LogFile", 0, win32con.REG_SZ, "")
        win32api.RegSetValueEx(freecap_main_keys, "LogLevel", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "LogTraffic", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "UDPHack", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "OneInstance", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "SysStartup", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "RunTray", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "MinimizeToTray", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "ResolveDNS", 0, win32con.REG_SZ, "2")
        win32api.RegSetValueEx(freecap_main_keys, "AddToCaptionText", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "ShowMessages", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "Language", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "HideOnClose", 0, win32con.REG_SZ, "1")
        win32api.RegSetValueEx(freecap_main_keys, "AdvancedHooking", 0, win32con.REG_SZ, "1")
        win32api.RegSetValueEx(freecap_main_keys, "SocksShare", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "SocksShareGuid", 0, win32con.REG_SZ, "")
        win32api.RegSetValueEx(freecap_main_keys, "SocksShareSharing", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_main_keys, "SocksSendLog", 0, win32con.REG_SZ, "1")
        win32api.RegSetValueEx(freecap_main_keys, "NavTreeWidth", 0, win32con.REG_SZ, "152")
        
        freecap_sock_keys = win32api.RegCreateKey(win32con.HKEY_CURRENT_USER, '''SoftWare\\Bert's Software\\FreeCap\\SocksChain\\00000001''')
        win32api.RegSetValueEx(freecap_sock_keys, "name", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_sock_keys, "Server", 0, win32con.REG_SZ, str(self.game_proxy_host))
        win32api.RegSetValueEx(freecap_sock_keys, "Port", 0, win32con.REG_SZ, str(self.game_proxy_port))
        win32api.RegSetValueEx(freecap_sock_keys, "Version", 0, win32con.REG_SZ, "5")
        win32api.RegSetValueEx(freecap_sock_keys, "Login", 0, win32con.REG_SZ, str(self.game_proxy_user))
        win32api.RegSetValueEx(freecap_sock_keys, "Socks4Ident", 0, win32con.REG_SZ, "")
        win32api.RegSetValueEx(freecap_sock_keys, "Password", 0, win32con.REG_SZ, str(self.game_proxy_pass))
        win32api.RegSetValueEx(freecap_sock_keys, "Auth", 0, win32con.REG_SZ, "1")
        win32api.RegSetValueEx(freecap_sock_keys, "HttpUser", 0, win32con.REG_SZ, "")
        win32api.RegSetValueEx(freecap_sock_keys, "HttpPass", 0, win32con.REG_SZ, "")
        win32api.RegSetValueEx(freecap_sock_keys, "HttpAuth", 0, win32con.REG_SZ, "0")
        win32api.RegSetValueEx(freecap_sock_keys, "InUse", 0, win32con.REG_SZ, "1")
        
        win32api.RegFlushKey(freecap_main_keys)
        win32api.RegFlushKey(freecap_sock_keys)
    
    def clean_build_conf(self):
        pass
    
    def make_cmdline(self):
        BASE_PATH = os.getcwd()
        if USE_PROXY:
            # write reg , configure freecap 
            self.build_conf()
            
            _cmd_line = '%s ' % PROXY_CAP
            _cmd_line += '-o f.log -l 15 '
            _cmd_line += '%s %s' % (GR.GAME_BASENAME, GR.GAME_ARGS)

        else:
            _cmd_line = '"%s"' % os.path.join(BASE_PATH, GR.GAME_BASENAME)+' '+GR.GAME_ARGS

        return _cmd_line
        
    def enter_game(self):
        '''when execut game program , then we use autoit, to control kay & mouse
        '''
        
        # build game runner command line
#===============================================================================
#        print "CWD: ", os.getcwd()
#        print "Script: ", sys.argv[0]
#        print ".EXE: ", os.path.dirname(sys.executable)
#        print "Script dir: ", os.path.realpath(os.path.dirname(sys.argv[0]))
#        pathname, scriptname = os.path.split(sys.argv[0])
#        print "Relative script dir: ", pathname
#        print "Script dir: ", os.path.abspath(pathname)
#===============================================================================
        GR.pre_check_display()
        
        run_game = wx.Process()
        _i = run_game.Open(self.make_cmdline(), wx.EXEC_ASYNC|wx.EXEC_NOHIDE)
        
        print self.game_acct_name, self.game_acct_pass, self.game_proxy_host, self.game_proxy_port, self.game_proxy_user, self.game_proxy_pass
        
        # send game login infomation 
        GR.GAME_ACCT_NAME = self.game_acct_name
        GR.GAME_ACCT_PASS = self.game_acct_pass
        
        # try auto enter game
        if GR.enter_game():
            pass
        else:
            return False

if __name__ == '__main__':
        g = GameRunner()
        g.build_conf()
        